package com.gitee.kamismile.stone.rpc.api.engine;

import com.gitee.kamismile.stone.rpc.api.IProtocol;
import com.gitee.kamismile.stone.rpc.api.invocation.support.StoneInvocation;
import com.gitee.kamismile.stone.rpc.common.engine.StoneAppEnum;
import com.gitee.kamismile.stone.rpc.common.errorenum.RpcErrorEnum;
import com.gitee.kamismile.stone.rpc.common.exception.RPCException;
import com.gitee.kamismile.stone.rpc.common.properties.StoneRpcConfigProperties;
import com.gitee.kamismile.stone.rpc.common.support.StoneURL;
import  org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import java.util.Objects;


/**
 * 容器启动初始化
 */
public class StoneAppEngine {
    protected final Logger logger = LoggerFactory.getLogger(StoneAppEngine.class);

    private StoneRpcConfigProperties stoneRpcConfigProperties;

    private IProtocol iProtocol;

    public StoneAppEngine(StoneRpcConfigProperties stoneRpcConfigProperties, IProtocol iProtocol) {
        this.iProtocol = iProtocol;
        this.stoneRpcConfigProperties = stoneRpcConfigProperties;
    }

    private StoneAppEnum stoneAppEnum = StoneAppEnum.DOWN;


    public StoneAppEnum getStoneAppEnum() {
        return stoneAppEnum;
    }


    public void ensureStatusUp() {
        StoneAppEnum currentStatus = getStoneAppEnum();
        if (!StoneAppEnum.UP.equals(currentStatus)) {
            String message =
                    "The request is rejected. Cannot process the request due to STATUS = " + currentStatus;
            logger.warn(message);
            throw new RPCException(RpcErrorEnum.DOWNSYSTEM.getCode(),RpcErrorEnum.DOWNSYSTEM.getMessage());
        }
    }


    public void start() {
        stoneAppEnum = StoneAppEnum.STARTING;
        StoneInvocation invocation = new StoneInvocation();
        StoneURL stoneURL = new StoneURL();
        stoneURL.setHost(stoneRpcConfigProperties.getHost());
        stoneURL.setPort(stoneRpcConfigProperties.getPort());
        stoneURL.setCoreThreads(stoneRpcConfigProperties.getCoreThreads());
        stoneURL.setMaxThreads(stoneRpcConfigProperties.getMaxThreads());
        stoneURL.setMaxIdleInSecond(stoneRpcConfigProperties.getMaxIdleInSecond());
        stoneURL.setMaxQueueSize(stoneRpcConfigProperties.getMaxQueueSize());
        stoneURL.getParameters().putAll(stoneRpcConfigProperties.getUserAttributes());
        invocation.setStoneURL(stoneURL);
        Runnable runnable = iProtocol.export(invocation);
        if (Objects.isNull(runnable)) {
            stoneAppEnum = StoneAppEnum.DOWN;
            return;
        }
        iProtocol.start(runnable);
        stoneAppEnum = StoneAppEnum.UP;
    }


    /**
     * 是否是消费端
     * @return
     */
    public boolean isServer(){
        if (Objects.isNull(stoneRpcConfigProperties.getHost())){
            return false;
        }
        return true;
    }


    /**
     * 优雅关机 调用销毁接口
     */
    public void destroy() {
        stoneAppEnum = StoneAppEnum.STOPPING;
        iProtocol.destroy();
        stoneAppEnum = StoneAppEnum.DOWN;
    }


}
